/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-05-13     金鹏       the first version
 */

#include "cmn_net.h"

rt_thread_t net_handl = RT_NULL;                /* 网络线程句柄*/
rt_mq_t net_recv_mq = RT_NULL;                  /* 网络消息队列句柄*/
extern int cm_switch;                           /* 0:WIFI; 1:4G*/
int net_flag = 0;                               /* 网络状态  0:初始状态 1:发送确认信息  2:接收到确认信息 3:已经断网*/

/*
* Net_Control_Entry
* @Param NULL
*
* Change Logs:
* Date           Author       Notes
* 2025-5-13      金鹏                      网络管理线程
*/
void Net_Control_Entry(void *parameter)
{
    char m100p_status_config[] = "config,get,netstatus,1\r\n";
    int flag_disconnect = 0;

    while(1){
        /* 初始状态或者接受成功状态*/
        if(net_flag == 0 || net_flag == 2){
            M100P_Sent(m100p_status_config);
            net_flag = 1;
            if(flag_disconnect == 1){
                rt_kprintf("4g connect\n");
                flag_disconnect = 0;
            }
        }/* 接收失败状态*/
        else{
            M100P_Sent(m100p_status_config);
            if(flag_disconnect == 0){
                rt_kprintf("4g disconnect\n");
                flag_disconnect = 1;
            }

            net_flag = 3;
        }

        rt_thread_mdelay(5000);
    }
}

/*
* Net_Control_Test
* @Param NULL
*
* Change Logs:
* Date           Author       Notes
* 2025-5-13      金鹏                      网络管理初始化
*/
void Net_Control_Test(void)
{
    /* 创建网路管理线程*/
    net_handl = rt_thread_create("net",
        Net_Control_Entry,RT_NULL,
        ENTRY_STACK_SIZE-1024,
        ENTRY_PRI,
        ENTRY_TICK);

    /* 创建wifi接收消息队列*/
    net_recv_mq = rt_mq_create("net_recv",
                              MQ_STACK_SIZE-40,   /* 20字节*/
                                   MQ_COUNT+10,   /* 30个*/
                                    MQ_FLAG);

    /* LOG*/
    if(net_recv_mq == RT_NULL){
        rt_kprintf("net recv mq create failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("net recv mq create successed\n");
    }

    /* LOG*/
    if(net_handl != RT_NULL){
        rt_kprintf("net thread create successful!!\n");

        /* 启动网络管理线程*/
        rt_thread_startup(net_handl);
    }else{
        rt_kprintf("net thread create failed!!\n");

        return -RT_ERROR;
    }

}
